/*
	StargateWW for GarrysMod10
	Copyright (C) 2008 Prefanatic

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

local meta = FindMetaTable("Player")

--################# Start the Derma Panel @Prefanatic
function StartDerma(ply, dermaStyle, starting)
	if dermaStyle == "info" then -- If we are using the info setup then send the usermessage.
		umsg.Start("infoMenu", ply);
			if ply:Team() >= 10 and ply:Team() < 19 then
				umsg.String("Team Atlantis")
			else
				umsg.String("Team Wraith")
			end
		umsg.End();
	elseif dermaStyle == "help" then -- If we are using the help setup then send the usermessage.
		umsg.Start("helpMenu", ply);
			umsg.Bool(starting);
		umsg.End();
	elseif dermaStyle == "team" then -- If we are using the team etup then send the usermessage.
		umsg.Start("teamMenu", ply);
		umsg.End();
	elseif dermaStyle == "upgrade" then
		umsg.Start("upgradeMenu", ply)
		umsg.End()
	end
	
	
end
concommand.Add ("startDerma", StartDerma)

function DamageSounds(ent)

	if ent:IsPlayer() then

		local num = math.random(10)
		
		if num == 1 then
	
			ent:EmitSound(table.Random(WW.Sounds.Ouch))
			
		end
		
	end

end
hook.Add("EntityTakeDamage", "DamageSounds", DamageSounds)

function GM:PlayerNoClip()

	return false
	
end

function SpawnWraith()

	local spawn = table.Random(WW.WraithSpawns)
	local spawnpos = spawn:GetPos()
	
	local wraith = ents.Create("ent_snpc_wraith")
		wraith:SetPos(spawnpos)
		wraith:Spawn()
		
end

concommand.Add("spawnwraith", SpawnWraith )

--################# Wraith Regeneration @Prefanatic
function WraithRegen()		
	for k, v in pairs(player.GetAll()) do -- get all the players
		if v:Team() >= 20 and v:Team() < 29 then -- if they are wraith.
			if v:Health() < 500 then -- and if their health is less than 500
		
				local wraithCurHealth = v:Health() -- grab their current health
				v:SetHealth(wraithCurHealth + 5) -- add 5 to their current health
			end
		end
	end
end

if Flavor == "Stargate Atlantis" then -- if we are running the atlantis flavor on the WW core
	timer.Simple(5, function() WraithRegen() end) -- create a timer, I think its better than think.
end

--################# Allow jumper to be flown by pilots @Prefanatic
function CheckPilotStatus(jumper, ply)
	if ply:HasGene() then return true end -- If he has the gene, then go.
end
hook.Add("StarGate.Player.EnterJumper", "CheckPilotStatus", CheckPilotStatus) -- hook into avon's jumper

--################# Change team @Prefanatic
function ChangeTeam(ply, t)
	for k,v in pairs(WWClass) do -- grab all the classes
		if v.Team == tonumber(t) then -- if it found the class then
			if ply:Team() == tonumber(t) then -- if you already are that class
				Notify(ply, "You already are a " .. v.Class .. "!\n", 5) -- notify of the problem
				return ""
			end
			if team.NumPlayers(tonumber(t)) == v.Max then -- if the class is full
				Notify(ply, "The team is full!  Also, how the hell did you bypass derma?", 5) -- notify of the problem
				return ""
			end
			
			ply:SetTeam(tonumber(t)) -- change the team
			ply:SetNWString("team", v.ClassArg) -- set the argument on the network
			ply:SetNWBool("OnClassChange", true) -- tell the client we are changing
			ply:KillSilent() -- kill
			ply:Spawn() -- spawn
			return
		end
	end
end

function GM:PlayerLoadout(ply)

	local T = ply:Team() -- current team
	
	ply:Give("weapon_physcannon") -- give basic items needed
	ply:Give("gmod_camera")
	
	if ply:IsAdmin() then -- if you are an admin, lets give you admin things.
		ply:Give("weapon_physgun")
		ply:Give("gmod_tool")
	end
	
	for k,v in pairs(WWClass) do -- go through the classes, if you are that class grab the wepaons from the class weapon selection and give them
		if v.Team == T then
			for k,v in pairs(v.Weapons) do
				ply:Give(v)
			end
		end
	end
	
end

--################# Spectate @Prefanatic
function Spectate(ply)
	players = player.GetAll() -- get all the players
	r = math.Rand(0, table.Count(players)) -- randomize the players
	
	ply:StripWeapons() -- strip if there are any weapons
	ply:Spectate(OBS_MODE_ROAMING) -- set chasing spectate
		--ply:SpectateEntity(players[r]) -- spectate the person
		ply:SetNWBool("spectating", true) -- tell the client we are spectating
		
end

--################# Check the upgrades @Prefanatic ----------- NEED TO FIX WITH NEW UPGRADE SYSTEM
function CheckUpgrades(ply)
	if ply:Team() >= 10 and ply:Team() < 19 then
		if ply:GetNWBool("ATLANTIS.UPGRADE.HEALTH") and not ply:GetNWBool(ply:Nick()..".UPGRADE.HEALTH") then
			ply:SetHealth(600)
			ply:ChatPrint("Setting Health Upgrade!")
		end
	end
	
	if ply:Team() >= 20 and ply:Team() < 29 then
		if ply:GetNWBool("WRAITH.UPGRADE.HEALTH") and not ply:GetNWBool(ply:Nick()..".UPGRADE.HEALTH") then
			ply:SetHealth(600)
			ply:ChatPrint("Setting Health Upgrade!")
		end
	end
	
	if ply:GetNWBool(ply:Nick()..".UPGRADE.HEALTH") then
		ply:SetHealth(600)
		ply:ChatPrint("Setting Health Upgrade!")
	end
end

HasntReloaded = true

function GM:PlayerSpawn(ply)
	self.BaseClass:PlayerSpawn(ply);
	ply:CrosshairEnable(); -- enable the crosshair
	ply:UnLock()
	ply:SetNWBool("Feede", false)
	ply:SetNWBool("Feeder", false)
	ply:SetNWBool("spectating", false)
	
	if ply:GetNWString("team") == "undecided" then -- if we are undesided, tell them to find something to do
		ply:PrintMessage(HUD_PRINTTALK, "You have not chosen a class, please do.");
		StartDerma(ply, "help", true);
		Spectate(ply)
		return
	end
	
	for k,v in pairs(WWClass) do -- go through the classes and set the models
		if v.Team == ply:Team() then
			ply:SetModel(v.Model)
		end
	end
	
	setspawnPos(ply) -- set the spawn possition
	RemoveDeathScreen(ply) -- remove the death screen
	CheckUpgrades(ply) -- check for upgrades
	
	if ply:GetNWBool("soundplay") == false then
		Notify( ply, WW.Flavor.JoinWelcome, 10 )
		RunConsoleCommand("play", table.Random(WW.Sounds.Join) )
		ply:SetNWBool("soundplay", true)
	end
	
	ply:CheckContentPack()
	
	local function WaitforCheckPack()
		if ply:GetNWBool("hascontentpack") == "false" then 
			Notify(ply, "You do not have the WW content pack, check your console!", 5)
			ply:PrintPack()
		end
	end
	timer.Simple(1, WaitforCheckPack)
	
	if ply:GetNWBool("creditsinit") == false then -- if the credits arn't initalized, initalize them.
		credits.Init(ply)
	end
end

function GetTeam(ply)
	if ply:Team() >= 10 and ply:Team() < 19 then
		return "atlantis"
	else
		return "wraith"
	end
end

function GM:PlayerInitialSpawn(ply)
	self.BaseClass:PlayerInitialSpawn(ply);
	ply:SetNWString("team", "undecided");
	ply:SetNWString("randcommand", WW.RandomServCall)
	ply:PrintMessage(HUD_PRINTTALK, "Welcome to StargateWW, created by Prefanatic.");
	datastream.StreamToClients(ply, "UpgradeTable", upgrades) -- send down the upgrades
	ply.WraithKilled = 0
end

function GM:PlayerDisconnected(ply)
	ply:KillSilent()
	ply:SetNWBool("soundplay", false)
	ply:SetNWBool("creditsinit", false)
	WW.Credits.Spend = false
end

function EnableTesting(ply, _, args)

	ply:SetNWBool("testing", true)
	ply:PrintMessage(HUD_PRINTCONSOLE, "World Wars: Testing enabled!")
	
end
concommand.Add("ww_testing", EnableTesting)